using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class C_Anim
{
    public string C_AnimName = "DefaultAnim";
    public bool C_IsDefaultAnim;
    public bool C_IsLoop;
    public AnimationClip C_AnimClip;
    public List<C_AnimEvent> C_AnimEventList = new List<C_AnimEvent>();
    public bool foldout = true;
    private AnimInfo animInfo;
    private Dictionary<AnimEventType, List<C_AnimEvent>> animEventDict = new Dictionary<AnimEventType, List<C_AnimEvent>>();
    private Dictionary<int, List<C_AnimEvent>> animEventFrameDict = new Dictionary<int, List<C_AnimEvent>>();

    public void InitAnimEvent()
    {
        animInfo = new AnimInfo(C_AnimClip.frameRate, C_AnimClip.length);
        for (int i = 0; i < animInfo.totalFrame; i++)
        {
            animEventFrameDict.Add(i, new List<C_AnimEvent>());
        }
        foreach (C_AnimEvent animEvent in C_AnimEventList)
        {
            if (!animEventDict.ContainsKey(animEvent.type))
            {
                animEventDict.Add(animEvent.type, new List<C_AnimEvent>());
            }
            animEventDict[animEvent.type].Add(animEvent);
            if ((int)animEvent.type < 2) return;
            else if (animEvent.type == AnimEventType.Interrupt)
            {
                if (animEvent.isSecond)
                {
                    int frame = (int)(Mathf.Min(animEvent.interruptTime, animInfo.length) / (1 / animInfo.frameRate));
                    animEventFrameDict[frame].Add(animEvent);
                }
                else
                {
                    if(!animEvent.isFrameRange)
                    {
                        int frame = Mathf.Min(animEvent.frameNumber, animInfo.totalFrame - 1);
                        animEventFrameDict[frame].Add(animEvent);
                    }
                    else
                    {
                        for(int i = (animEvent.isAllFrameRange ? 0 : (int)animEvent.frameRange[0]); i <= (animEvent.isAllFrameRange ? animInfo.totalFrame - 1 : (int)animEvent.frameRange[1]);i++)
                        {
                            animEventFrameDict[i].Add(animEvent);
                        }
                    }
                }
            }
            else
            {
                if (!animEvent.isFrameRange)
                {
                    int frame = Mathf.Min(animEvent.frameNumber, animInfo.totalFrame - 1);
                    animEventFrameDict[frame].Add(animEvent);
                }
                else
                {
                    for (int i = (animEvent.isAllFrameRange ? 0 : (int)animEvent.frameRange[0]); i <= (animEvent.isAllFrameRange ? animInfo.totalFrame - 1 : (int)animEvent.frameRange[1]); i++)
                    {
                        animEventFrameDict[i].Add(animEvent);
                    }
                }
            }
        }
    }
    public List<C_AnimEvent> GetAnimEventByType(AnimEventType type)
    {
        if (animEventDict.ContainsKey(type))
        {
            return animEventDict[type];
        }
        return null;
    }
    public Dictionary<int, List<C_AnimEvent>> GetAnimEventFrameDict()
    {
        return animEventFrameDict;
    }
    public AnimInfo GetAnimInfo()
    {
        return animInfo;
    }
}
public class AnimInfo
{
    public float frameRate;
    public float length;
    public float secondPerFrame;
    public int totalFrame;

    public AnimInfo(float frameRate, float length)
    {
        this.frameRate = frameRate;
        this.length = length;
        this.secondPerFrame = 1/frameRate;
        totalFrame = (int)(length / (1 / frameRate));
    }
}
